package com.auto.learning.tools;

import java.util.ArrayList;
import java.util.List;

import com.auto.learning.bean.CaseType;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;

@SuppressLint("NewApi")
public class Utils {
	public static int getAndroidSDKVersion() {
        int version = 0;
        try {
            version = Integer.valueOf(android.os.Build.VERSION.SDK);
        } catch (NumberFormatException e) {
         
        }
        return version;
    }
	 /** 
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 
     */  
    public static int dip2px(Context context, float dpValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (dpValue * scale + 0.5f);  
    }  
  
    /** 
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp 
     */  
    public static int px2dip(Context context, float pxValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (pxValue / scale + 0.5f);  
    }  
    
    public static List<Button> setBookType(LinearLayout lin,List<CaseType> typelist,Context context){
    	List<Button>  buttons = new ArrayList<Button>();
		lin.removeAllViews();
		lin.setWeightSum((float)typelist.size());
		for (int i = 0; i < typelist.size(); i++) {
			Button button=new Button(context);			
			if(getAndroidSDKVersion()>15){
				button.setBackground(context.getResources().getDrawable(typelist.get(i).getDrawable()));
			}else{
				button.setBackgroundDrawable(context.getResources().getDrawable(typelist.get(i).getDrawable()));
			}
			button.setPadding(0, 0, 0, 0);
			button.setText(typelist.get(i).getText());
			button.setTextSize(dip2px(context, 5));
			button.setGravity(Gravity.CENTER);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1f); 
			if(typelist.size()>1){
				if(i==0){
					lp.setMargins(0,0,Utils.dip2px(context,6),0);
				}else{
					lp.setMargins(Utils.dip2px(context, 6),0,0,0);
				}
			}
			button.setLayoutParams(lp);
			button.setOnClickListener(typelist.get(i).getClick());
			lin.addView(button);
			buttons.add(button);
		}
		return buttons;
	}
}
